package com.cjjd.c.feature.subscribe.info;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.ActionBarActivity;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.bean.subscribe.HotelDetailAndDevice;
import com.cjjd.c.bean.subscribe.HotelDetailAndDevice.HotelFacilityShowModel;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.SharePrefUtil;
import com.cjjd.c.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * ----------------------------------
 * Created by xyd on 2017/12/19.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class HotelDetailAndDeviceActivity extends ActionBarActivity {
    @Bind(R.id.gridview)
    GridView gridview;
    @Bind(R.id.tv_description)
    TextView tvDescription;
    @Bind(R.id.btn_call_hotel)
    Button btnCallHotel;


    private String hotelUuid = "";
    private ArrayList<HotelFacilityShowModel> dataList;
    private HotelDetailAndDeviceAdapter hotelDetailAndDeviceAdapter;
    private String hotelPhone = "";

    @Override
    public Object getTitleString() {
        return "详情设施";
    }

    @Override
    public int getContentView() {
        return R.layout.activity_detail_device;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initView();
        hotelUuid = getIntent().getStringExtra("hotelUuid");
        getData();
    }

    private void getData() {
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("hotelUuid", hotelUuid);


        Call<HttpResult<HotelDetailAndDevice>> call = service.getHotelInfoAndFacility(JsonUtils.convertParam("getHotelInfoAndFacility", h1));
        call.enqueue(new CallbackImpl<HotelDetailAndDevice>() {
            @Override
            public void responseSuccess(Call<HttpResult<HotelDetailAndDevice>> call, Response<HttpResult<HotelDetailAndDevice>> response) {

                if (response.body().getData() != null) {
                    dataList.addAll(response.body().getData().dataList);
                    hotelDetailAndDeviceAdapter.notifyDataSetChanged();

                    hotelPhone = response.body().getData().hotelPhone;
                    btnCallHotel.setText("酒店电话：" + hotelPhone);

                    tvDescription.setText(response.body().getData().hotelInfo);

                }
            }
        });


    }

    private void initView() {

        dataList = new ArrayList<>();


        hotelDetailAndDeviceAdapter = new HotelDetailAndDeviceAdapter(mContext);
        hotelDetailAndDeviceAdapter.setData(dataList);
        gridview.setAdapter(hotelDetailAndDeviceAdapter);

    }


    @OnClick(R.id.btn_call_hotel)
    public void onViewClicked() {
        // TODO: 2017/12/19 拨打电话
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + hotelPhone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            ToastUtil.showShortToast(mContext,"请同意用户电话权限");
            return;
        }
        mContext.startActivity(intent);
    }
}
